package 笔试.快手;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * @author:谢君臣
 * @Date:2021/9/11-18:32
 * @version:1.0
 * @function:
 */
public class Main03 {
    public static void main(String[] args) {
        sort(new String[]{"A", "B", "C", "D", "E", "F"},new String[][]{{"C", "F"}, {"C", "D"}, {"E"}, {"E"}, {}, {}});
    }
    public static String[] sort (String[] modNames, String[][] modDepends) {
        // write code here
        HashMap<String,Integer> hashMap = new HashMap<>();
        HashMap<String,Integer> index = new HashMap<>();
        for (int i = 0; i < modNames.length; i++) {
            index.put(modNames[i],i);
            hashMap.put(modNames[i],0);
        }
        int [][]dp = new int[modNames.length][modNames.length];
        for (int i = 0; i < modNames.length; i++) {
            String father_key = modNames[i];
            int temp = hashMap.getOrDefault(father_key,0);
            for (int j = 0; j < modDepends[i].length; j++) {
                String key = modDepends[i][j];
                hashMap.put(key, temp + hashMap.getOrDefault(key,0)+1);
               dp[i][index.get(key)] = 1;
            }
        }
        List<node> pre = new ArrayList<>();
        for (int i = 0; i < modNames.length; i++) {
            pre.add(new node(modNames[i],hashMap.get(modNames[i])));
        }
        pre.sort(new Comparator<node>() {
            @Override
            public int compare(node o1, node o2) {
                node n1 = (node)o1;
                node n2 = (node)o2;
                return n2.val.compareTo(n1.val);
            }
        });
        for (int i = 0; i < modNames.length; i++) {
            modNames[i] = pre.get(i).key;
        }
        // 解决循环引用问题


        // 解决非依赖的先后顺序问题



        return modNames;

    }
    static class node{
        String key;
        public Integer val;

        public node(String key, int val) {
            this.key = key;
            this.val = val;
        }
    }
}
